<?php
namespace Common\Model\Common;

use Common\Base\ModelBase;

class ArticleModel extends ModelBase
{
    /**
     * 得到数据
     *
     * @param int    $w         1:计数;  2:数据  3:计数&数据  4:一条数据
     * @param array  $search
     * @param string $fields
     * @param int    $page
     * @param int    $pageSize
     * @param string $by
     *
     * @return array
     */
    public function getCategory($w=3, $search=array(), $fields='*', $page=0, $pageSize=0, $by='')
    {
        //查询条件
        $where =  ' where 1 ';
        if( ! empty($search))
        {
            isset($search['id']) && intval($search['id'])>0 && $where .= " and id=".intval($search['id']);
            isset($search['id_neq']) && intval($search['id_neq'])>0 && $where .= " and id!=".intval($search['id_neq']);
            isset($search['fid']) && intval($search['fid'])>0 && $where .= " and fid=".intval($search['fid']);
            isset($search['category_name']) && $search['category_name']!='' && $where .= " and category_name='".$search['category_name']."'";
            isset($search['category_name_like']) && $search['category_name_like']!='' && $where .= " and category_name like '%".$search['category_name_like']."%'";
            isset($search['path_like']) && $search['path_like']!='' && $where .= " and path like '%,".$search['path_like'].",%'";
            isset($search['st']) && intval($search['st'])>0 && $where .= " and st=".intval($search['st']);
        }

        //排序
        $orderBy = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderBy .= 'id asc';
        }else{
            $orderBy .= $by;
        }

        //SQL
        $sql['sql_ct'] = 'select count(id) as tot from '.getTable('common_article_category').$where;
        $sql['sql']    = 'select '.$fields.' from '.getTable('common_article_category').$where.$orderBy;
        //ddd($sql,1);

        return $this->getData($w, $sql, $page, $pageSize);
    }

    /**
     * 添加
     *
     * @param array $dat
     *
     * @return mixed
     */
    public function addCategory($dat = array())
    {
        return $this->addRow($dat, getTable('common_article_category'));
    }

    /**
     * 编辑
     *
     * @param array $where
     * @param array $dat
     *
     * @return mixed
     */
    public function editCategory($where=array(), $dat=array())
    {
        return $this->updateRow($where, $dat, getTable('common_article_category'));
    }

    /**
     * 删除
     *
     * @param array $where
     *
     * @return mixed
     */
    public function delCategory($where = array())
    {
        return $this->delRow($where, getTable('common_article_category'));
    }

    /**
     * 得到数据
     *
     * @param int    $w
     * @param array  $search
     * @param string $fields
     * @param int    $page
     * @param int    $pageSize
     * @param string $by
     *
     * @return array
     */
    public function getTableArticle($w=3, $search=array(), $fields='*', $page=0, $pageSize=0, $by='')
    {
        //查询条件
        $where =  ' where 1 ';
        if( ! empty($search))
        {
            isset($search['id']) && intval($search['id'])>0 && $where .= " and id=".intval($search['id']);
            isset($search['cid']) && intval($search['cid'])>0 && $where .= " and cid=".intval($search['cid']);
            isset($search['title_like']) && $search['title_like']!='' && $where .= " and title like '%".$search['title_like']."%'";
            isset($search['st']) && intval($search['st'])>-1 && $where .= " and st=".intval($search['st']);
            isset($search['cid_st']) && intval($search['cid_st'])>-1 && $where .= " and cid_st=".intval($search['cid_st']);
        }

        //排序
        $orderBy = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderBy .= 'id desc';
        }else{
            $orderBy .= $by;
        }

        //SQL
        $sql['sql_ct'] = 'select count(id) as tot from '.getTable('common_article').$where;
        $sql['sql']    = 'select '.$fields.' from '.getTable('common_article').$where.$orderBy;
        //ddd($sql,1);

        return $this->getData($w, $sql, $page, $pageSize);
    }

    /**
     * 得到数据
     *
     * @param int    $w
     * @param array  $search
     * @param string $fields
     * @param int    $page
     * @param int    $pageSize
     * @param string $by
     *
     * @return array
     */
    public function getArticle($w=3, $search=array(), $fields='a.*,b.category_name', $page=0, $pageSize=0, $by='')
    {
        //查询条件
        $where =  ' where 1 ';
        if( ! empty($search))
        {
            isset($search['id']) && intval($search['id'])>0 && $where .= " and a.id=".intval($search['id']);
            isset($search['cid']) && intval($search['cid'])>0 && $where .= " and a.cid=".intval($search['cid']);
            isset($search['title_like']) && $search['title_like']!='' && $where .= " and a.title like '%".$search['title_like']."%'";
            isset($search['st']) && intval($search['st'])>-1 && $where .= " and a.st=".intval($search['st']);
            isset($search['cid_st']) && intval($search['cid_st'])>-1 && $where .= " and a.cid_st=".intval($search['cid_st']);
        }

        //排序
        $orderBy = ' order by ';
        $by = trim($by);
        if($by == ''){
            $orderBy .= 'a.id desc';
        }else{
            $orderBy .= $by;
        }

        //SQL
        $sql['sql_ct'] = 'select count(a.id) as tot from '.getTable('common_article').' as a left join '.getTable('common_article_category').' as b on a.cid=b.id '.$where;
        $sql['sql']    = 'select '.$fields.' from '.getTable('common_article').' as a left join '.getTable('common_article_category').' as b on a.cid=b.id '.$where.$orderBy;
        //ddd($sql,1);

        return $this->getData($w, $sql, $page, $pageSize);
    }

    /**
     * 添加
     *
     * @param array $dat
     *
     * @return mixed
     */
    public function addArticle($dat = array())
    {
        return $this->addRow($dat, getTable('common_article'));
    }

    /**
     * 编辑
     *
     * @param array $where
     * @param array $dat
     *
     * @return mixed
     */
    public function editArticle($where=array(), $dat=array())
    {
        return $this->updateRow($where, $dat, getTable('common_article'));
    }

    /**
     * 删除
     *
     * @param array $where
     *
     * @return mixed
     */
    public function delArticle($where = array())
    {
        return $this->delRow($where, getTable('common_article'));
    }
}